<template>
  <div class="login-page">
    <NavBar>
      <template #default> 用户登录 </template>
    </NavBar>
    <div class="container">
      <div class="forms-container">
        <div class="login-title">用户登录</div>
        <el-form
          ref="lbLogin"
          :model="form"
          label-width="80px"
          :rules="rules"
          class="loginForm"
        >
          <el-form-item label="用户名" prop="name">
            <el-input v-model="form.name" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="form.password"
              placeholder="请输入密码"
              type="password"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="submit-btn"
              @click="onSubmit('lbLogin')"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
        <!-- 找回密码 -->
        <div class="tiparea">
          <p>
            忘记密码?<a href="#">立即找回</a>&nbsp;&nbsp;
            <router-link to="/register"> 注册 </router-link>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import NavBar from "../../components/common/NavBar.vue";
import { ElMessage } from "element-plus"; // 引入消息提示组件

export default {
  components: {
    NavBar,
  },
  setup() {
    const state = reactive({
      form: {
        name: "",
        password: "",
      },
      rules: {
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    });
    const router = useRouter();
    const { ctx } = getCurrentInstance(); // 获取当前实例
    const onSubmit = (formName) => {
      ctx.$refs[formName].validate((valid) => {
        if (valid) {
          // 登录成功，跳转到首页
          // 访问后端接口
          // TODO: 登录接口
          // 登录成功，跳转到首页
          localStorage.setItem("username", state.form.name);
          ElMessage.success("登录成功");
          router.push("/");
        } else {
          ElMessage.error("用户名或密码错误");
          return false;
        }
      });
    };
    return {
      ...toRefs(state),
      onSubmit,
    };
  },
};
</script>

<style scoped>
/* 页面背景 */
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f7f7f7;
  padding: 20px;
}

/* 主容器 */
.container {
  width: 100%;
  max-width: 400px; /* 限制最大宽度 */
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

/* 登录框标题 */
.login-title {
  text-align: center;
  font-size: 1.5rem;
  font-weight: bold;
  margin-bottom: 30px;
  color: #409eff;
}

/* 登录表单 */
.loginForm {
  width: 100%;
}

/* 按钮 */
.submit-btn {
  width: 100%;
  height: 45px;
  font-size: 1.2rem;
}

/* 找回密码区域 */
.tiparea {
  text-align: right;
  font-size: 12px;
  color: #666;
  margin-top: 20px;
}

.tiparea p a {
  color: #409eff;
  text-decoration: none;
}

.tiparea p a:hover {
  text-decoration: underline;
}

/* 输入框 */
.el-input {
  height: 40px;
  font-size: 1rem;
}

.el-form-item {
  margin-bottom: 20px;
}

/* 自定义 ElMessage 的样式，使其更醒目 */
.custom-success-message {
  font-size: 1.5rem; /* 字体大小 */
  font-weight: bold; /* 加粗 */
  color: #409eff; /* 颜色 */
  background-color: #fff; /* 背景色 */
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2); /* 阴影效果 */
  padding: 15px; /* 内边距 */
  border-radius: 8px; /* 圆角 */
}
</style>
